//index.js
const EventEmitter = require('../../common/EventEmitter.js');
const requester = require('../../common/requester.js');
const util = require('../../common/util.js');
const $$onLoad = require('../../appLoaded/onLoad');
const app = getApp();

Page({
    pageIndex: 1,
    data: {
        userInfo: {},
        actions: ['addImage'],
        imageList: '',
        isAudit: true,
        isAuthed: true,
        isRefreshing: false
    },

    onLoad: function () {
        $$onLoad()
            .then(() => {
                let isAudit = app.globalData.isAudit,
                    isAuthed = app.globalData.isAuthed;

                this.setData({
                    isAudit,
                    isAuthed
                });

                if (isAuthed) {
                    this.init();
                }
            })
    },

    init: function () {
        this.setData({
            isAuthed: true
        });
        this.getData(false);
        EventEmitter.addListener('backFormSend', () => {
            this.getData(false);
        });
    },
    onShow: function (param) {

    },

    onPullDownRefresh: function () {
        console.log("onPullDownRefresh.....");
        this.initData();
    },

    onPullDownStop: function () {
        wx.stopPullDownRefresh();
    },

    onReachBottom: function () {
        console.log("onReachBottom.....");
        this.getData(true);
    },

    initData: function () {
        this.getData(false);
    },

    /**
     * 获取数据。
     * @param isLoadMore boolean 是否是加载更多。
     */
    getData: function (isLoadMore) {
        let me = this;

        if (isLoadMore) {
            this.pageIndex++;
            wx.showLoading({
                title: '加载中...'
            });
        } else {
            this.pageIndex = 1;
        }

        this.getResourceList()
            .then(resourceLi => {
                    if (resourceLi) {
                        util.addModeToList(resourceLi);
                        if (isLoadMore) {
                            let tmpLi = me.data.imageList || [];
                            resourceLi = tmpLi.concat(resourceLi)
                        }
                        me.setData({
                            imageList: resourceLi
                        })
                    }
                    wx.hideLoading();
                    this.onPullDownStop();
                }
            );
    },


    /**
     * 获取资源列表
     */
    getResourceList: function () {
        let pageIndex = this.pageIndex;
        return requester.request({
            url: requester.service.resourceList,
            data: {
                pageIndex: pageIndex
            }
        }).then(res => {
            return res.data.groupList;
        });
    },

    // 单个图片点击
    cellTap: function (e) {
        // Store the imageList
        if (this.data.isAudit) {
            return;
        }
        const curImage = e.currentTarget.dataset.image;
        wx.navigateTo({
            url: '../pictureDetail/pictureDetail?' + "id=" + curImage.id
        });
    },

    addImage: function (e) {
        wx.chooseImage({
            count: 1,
            success: (res) => {
                let tempFilePath = res.tempFilePaths[0];
                wx.navigateTo({
                    url: `../send/send?url=${tempFilePath}&from=main`
                });
            }
        });
    },

    onShareAppMessage: function (res) {
        return {
            title: '小画廊',
            path: '/pages/pictureList/pictureList',
        }
    },
    setRandomBack: function () {
        const colors = backgroudColor.colors,
            len = colors.length,
            random = Math.floor(Math.random() * len);
        return colors[random];
    }
});
